package com.xuelang.pipeline.artifact.app.nexus3v1.fallback;

import com.xuelang.pipeline.artifact.app.nexus3v1.RepositoryManagementService;
import com.xuelang.pipeline.artifact.app.nexus3v1.exception.ArtifactException;
import com.xuelang.pipeline.artifact.app.nexus3v1.request.*;
import com.xuelang.pipeline.artifact.app.nexus3v1.response.ListRepositoryResponse;
import com.xuelang.pipeline.artifact.app.nexus3v1.xo.ApiResponseXO;
import org.springframework.cloud.openfeign.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author xiwen.zxg
 * @since 2024/7/10
 */
@Slf4j
@Service
public class RepositoryManagementServiceFallbackFactory implements FallbackFactory<RepositoryManagementService> {
    @Override
    public RepositoryManagementService create(Throwable throwable) {
        ApiResponseXO<Void> apiResponseXO = new ApiResponseXO<>();
        if (throwable instanceof ArtifactException) {
            ArtifactException nexus3Exception = (ArtifactException) throwable;
            apiResponseXO = nexus3Exception.getApiResponse();
        }
        final ApiResponseXO resultApiResponseXO = ApiResponseXO.builder()
                .status(apiResponseXO.getStatus())
                .id(apiResponseXO.getId())
                .message(apiResponseXO.getMessage())
                .data(apiResponseXO.getData())
                .build();
        log.error("{} happened to exception with {}", this.getClass().getName(), throwable.getMessage());
        return new RepositoryManagementService() {

            @Override
            public ApiResponseXO<List<ListRepositoryResponse>> listRepository() {
                return resultApiResponseXO;
            }

            @Override
            public ApiResponseXO<Void> deleteRepositoryOfAnyFormat(String repositoryName) {
                return resultApiResponseXO;
            }

            @Override
            public ApiResponseXO<Void> createMavenGroupRepository(CreateMavenGroupRequest request) {
                return resultApiResponseXO;
            }

            @Override
            public ApiResponseXO<Void> updateMavenGroupRepository(String repositoryName, CreateMavenGroupRequest request) {
                return resultApiResponseXO;
            }

            @Override
            public ApiResponseXO<Void> createMavenHostedRepository(CreateMavenHostedRequest request) {
                return resultApiResponseXO;
            }

            @Override
            public ApiResponseXO<Void> createMavenProxyRepository(CreateMavenProxyRequest request) {
                return resultApiResponseXO;
            }

            @Override
            public ApiResponseXO<Void> createNpmGroupRepository(CreateNpmGroupRequest request) {
                return resultApiResponseXO;
            }

            @Override
            public ApiResponseXO<Void> updateNpmGroupRepository(String repositoryName, CreateNpmGroupRequest request) {
                return resultApiResponseXO;
            }

            @Override
            public ApiResponseXO<Void> createNpmHostedRepository(CreateNpmHostedRequest request) {
                return resultApiResponseXO;
            }

            @Override
            public ApiResponseXO<Void> createNpmProxyRepository(CreateNpmProxyRequest request) {
                return resultApiResponseXO;
            }

            @Override
            public ApiResponseXO<Void> createDockerGroupRepository(CreateDockerGroupRequest request) {
                return resultApiResponseXO;
            }

            @Override
            public ApiResponseXO<Void> updateDockerGroupRepository(String repositoryName, CreateDockerGroupRequest request) {
                return resultApiResponseXO;
            }

            @Override
            public ApiResponseXO<Void> createDockerHostedRepository(CreateDockerHostedRequest request) {
                return resultApiResponseXO;
            }

            @Override
            public ApiResponseXO<Void> createDockerProxyRepository(CreateDockerProxyRequest request) {
                return resultApiResponseXO;
            }

            @Override
            public ApiResponseXO<Void> createRawGroupRepository(CreateRawGroupRequest request) {
                return resultApiResponseXO;
            }

            @Override
            public ApiResponseXO<Void> updateRawGroupRepository(String repositoryName, CreateRawGroupRequest request) {
                return resultApiResponseXO;
            }

            @Override
            public ApiResponseXO<Void> createRawHostedRepository(CreateRawHostedRequest request) {
                return resultApiResponseXO;
            }

            @Override
            public ApiResponseXO<Void> createRawProxyRepository(CreateRawProxyRequest request) {
                return resultApiResponseXO;
            }
        };
    }
}
